<template>
  <list-card>
    <el-form v-show="showSearch" ref="queryForm" slot="search" :inline="true" :model="queryParams" class="search-form"
             label-suffix="：" size="small">
      <el-form-item label="任务名称" prop="jobName">
        <el-input v-model="queryParams.jobName" clearable placeholder="请输入任务名称"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="执行状态" prop="status">
        <el-select v-model="queryParams.status" clearable placeholder="请选择执行状态">
          <el-option v-for="dict in localDict.quartz_result" :key="dict.value" :label="dict.label"
                     :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="执行时间">
        <el-date-picker v-model="dateRange" :default-time="['00:00:00', '23:59:59']" end-placeholder="结束日期"
                        range-separator="-" start-placeholder="开始日期" style="width: 240px" type="daterange"
                        value-format="yyyy-MM-dd" @change="handleChangeDate"/>
      </el-form-item>
      <el-form-item class="search-btn">
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" type="info" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <div slot="operator">
      <el-button :disabled="multiple" icon="el-icon-delete" size="mini" type="danger" @click="handleDelete">删除
      </el-button>
      <el-button icon="el-icon-delete" size="mini" type="danger" @click="handleClean">清空</el-button>
      <el-button icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      <el-button icon="el-icon-close" size="mini" @click="handleClose">关闭</el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </div>
    <el-table slot="list" v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="40"/>
      <el-table-column label="任务名称" min-width="150px" prop="jobName" show-overflow-tooltip/>
      <el-table-column label="任务组名" min-width="100px" prop="jobGroup">
        <template v-slot:default="scope">
          <dict-tag :options="localDict.quartz_group" :value="scope.row.jobGroup" show-label/>
        </template>
      </el-table-column>
      <el-table-column label="调用目标字符串" min-width="160px" prop="invokeTarget" show-overflow-tooltip/>
      <el-table-column label="执行状态" min-width="100px" prop="status">
        <template v-slot:default="scope">
          <dict-tag :options="localDict.quartz_result" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="执行时间" min-width="180px" prop="createTime">
        <template v-slot:default="scope">
          <span>{{ scope.row.startTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="日志信息" min-width="230" prop="jobMessage" show-overflow-tooltip/>
      <el-table-column fixed="right" label="操作" min-width="120px">
        <template v-slot:default="scope">
          <el-button icon="el-icon-view" size="mini" type="text" @click="handleView(scope.row)">详细</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="pageParam.total>0" slot="page" :limit.sync="pageParam.pageSize" :page.sync="pageParam.pageNum"
                :total="pageParam.total" @pagination="getList"/>

    <!-- 调度日志详细 -->
    <el-dialog :visible.sync="dialog.open" append-to-body title="调度日志详细" width="700px">
      <el-form ref="form" :model="formdata" label-width="100px" size="mini">
        <el-row>
          <el-col :span="12">
            <el-form-item label="日志序号">{{ formdata.id }}</el-form-item>
            <el-form-item label="任务名称">{{ formdata.jobName }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务分组">
              <dict-tag :options="localDict.quartz_group" :value="formdata.jobGroup" show-label/>
            </el-form-item>
            <el-form-item label="执行时间">{{ formdata.startTime }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="调用方法">{{ formdata.invokeTarget }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="日志信息">{{ formdata.jobMessage }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="执行状态">
              <dict-tag :options="localDict.quartz_result" :value="formdata.status" show-label/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item v-if="formdata.status === 1" label="异常信息">{{ formdata.exceptionInfo }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog.open = false">关 闭</el-button>
      </div>
    </el-dialog>
  </list-card>
</template>

<script>
// import {cleanJobLog} from "@/api/monitor/jobLog";
import moment from "moment";
import {list, remove} from "@/api/base";
import {getStaticData} from "@/api/common";

export default {
  name: "JobLog",
  dicts: ['sys_common_status', 'sys_job_group'],
  data() {
    return {
      // 选中数组
      ids: [],
      // 遮罩层
      baseUrl: '/monitor/job/log/',
      loading: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 调度日志表格数据
      dataList: [],
      dateRange: [],
      // 查询参数
      queryParams: {
        jobName: undefined,
        jobGroup: undefined,
        status: undefined,
        params: {
          executeTime_begin: undefined,
          executeTime_end: undefined,
        }
      },
      pageParam: {
        // 总条数
        total: 0,
        pageNum: 1,
        pageSize: 10,
      },
      localDict: {
        quartz_group: [],
        quartz_result: [],
        quartz_status: [],
      },
      dialog: {
        // 是否显示弹出层
        open: false,
        title: '',
      },
      // 表单参数
      formdata: {},
    };
  },
  created() {
    this.getLocalDict()
    this.resetQuery()
  },
  methods: {
    getLocalDict() {
      this.getStaticData('system-quartz-group').then(res => {
        this.localDict.quartz_group = res.data
      })
      this.getStaticData('system-quartz-status').then(res => {
        this.localDict.quartz_status = res.data
      })
      this.getStaticData('system-quartz-result').then(res => {
        this.localDict.quartz_result = res.data
      })
    },
    /** 查询调度日志列表 */
    getList() {
      this.loading = true;
      list(this.baseUrl, {
        ...this.queryParams,
        pageNum: this.pageParam.pageNum,
        pageSize: this.pageParam.pageSize
      }).then(response => {
        this.dataList = response.rows;
        this.pageParam.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageParam.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      console.log(this.$route)
      const jobId = this.$route.query.id;
      if (jobId !== undefined && jobId !== 0) {
        this.$set(this.queryParams, "jobId", jobId)
        this.$set(this.queryParams, "jobName", this.$route.query.name)
        this.$set(this.queryParams, "jobGroup", this.$route.query.group)
      }
      this.$set(this.queryParams, "executeTime_begin", moment().startOf("month").format("YYYY-MM-DD HH:mm:ss"))
      this.$set(this.queryParams, "executeTime_end", moment().endOf("day").format("YYYY-MM-DD HH:mm:ss"))
      this.dateRange = [moment().startOf('month').format("YYYY-MM-DD"), moment().format("YYYY-MM-DD")]
      this.handleQuery();
    },
    handleChangeDate(val) {
      if (val === null) {
        this.$modal.msgWarning("执行时间不能为空");
        return
      }
      this.queryParams.executeTime_begin = this.dateRange[0];
      this.queryParams.executeTime_end = this.dateRange[1];
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.multiple = !selection.length;
    },
    /** 详细按钮操作 */
    handleView(row) {
      this.dialog.open = true;
      this.formdata = row;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除调度日志编号为"' + ids + '"的数据项？').then(() => {
        remove(this.baseUrl, ids, ids.length > 1).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      });
    },
    /** 清空按钮操作 */
    handleClean() {
      this.$modal.confirm('是否确认清空所有调度日志数据项？').then(() => {
        cleanJobLog().then(() => {
          this.getList();
          this.$modal.msgSuccess("清空成功");
        }).catch(() => {
        })
      });
    },
    // 返回按钮
    handleClose() {
      const obj = {path: "/monitor/job"};
      this.$tab.closeOpenPage(obj);
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/monitor/jobLog/exportData', {
        ...this.queryParams
      }, `任务日志导出_${moment().format('YYYY-MM-DD')}.xlsx`, 'application/json;charset=utf-8')
    }
  }
};
</script>
